﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class DlgRoomTypeManage : System.Windows.Forms.Form
	{
		public DlgRoomTypeManage()
		{
			this.InitializeComponent();
		}

		public DlgRoomTypeManage(string industry, string buildingType, string place, string id)
		{
			this.InitializeComponent();
			this.m_userIndustry = industry;
			this.m_userBuildingType = buildingType;
			this.m_userPlace = place;
			this.m_userId = (string.IsNullOrWhiteSpace(id) ? -1 : Convert.ToInt32(id));
		}

		public BuildingPlaceData RoomTypeData
		{
			get
			{
				return this.m_dataPlace;
			}
		}

		private void DlgRoomTypeManage_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitButtonTip();
			this.InitTreeView();
			this.EnableButtonSave(false);
			this.SetUserSelectedNode();
			this.SetUserSelectedPlace();
		}

		private void InitButtonTip()
		{
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAdd, "添加");
			toolTip.SetToolTip(this.btnDelete, "删除");
		}

		private void InitTreeView()
		{
			if (this.m_dataList == null || this.m_dataList.Count <= 0)
			{
				this.m_dataList = BuildingPlaceDb.ThisDb.GetDatas();
			}
			List<TreeNode> list = new List<TreeNode>();
			foreach (string text in this.GetIndustryTypes())
			{
				TreeNode item = this.treeView.Nodes.Add(text);
				list.Add(item);
			}
			foreach (TreeNode treeNode in list)
			{
				foreach (string text2 in this.GetBuildingTypes(treeNode.Text))
				{
					treeNode.Nodes.Add(text2);
				}
			}
			this.treeView.SelectedNode = this.treeView.Nodes[0].FirstNode;
		}

		private List<string> GetIndustryTypes()
		{
			List<string> list = new List<string>();
			if (this.m_dataList == null || this.m_dataList.Count <= 0)
			{
				return list;
			}
			foreach (BuildingPlaceData buildingPlaceData in this.m_dataList)
			{
				if (!list.Contains(buildingPlaceData.Industry))
				{
					list.Add(buildingPlaceData.Industry);
				}
			}
			return list;
		}

		private List<string> GetBuildingTypes(string text)
		{
			List<string> list = new List<string>();
			if (this.m_dataList == null || this.m_dataList.Count <= 0)
			{
				return list;
			}
			foreach (BuildingPlaceData buildingPlaceData in this.m_dataList)
			{
				if (string.Compare(buildingPlaceData.Industry, text) == 0 && !list.Contains(buildingPlaceData.BuildingType))
				{
					list.Add(buildingPlaceData.BuildingType);
				}
			}
			return list;
		}

		private List<BuildingPlaceData> GetBuildingPlaces(string industry, string buildingType)
		{
			List<BuildingPlaceData> list = new List<BuildingPlaceData>();
			if (this.m_dataList == null || this.m_dataList.Count <= 0)
			{
				return list;
			}
			foreach (BuildingPlaceData buildingPlaceData in this.m_dataList)
			{
				if (string.Compare(industry, buildingPlaceData.Industry) == 0 && string.Compare(buildingType, buildingPlaceData.BuildingType) == 0)
				{
					list.Add(buildingPlaceData);
				}
			}
			return list;
		}

		private void RefreshDataGridView()
		{
			string text = string.Empty;
			string text2 = string.Empty;
			TreeNode selectedNode = this.treeView.SelectedNode;
			if (selectedNode == null)
			{
				return;
			}
			if (selectedNode.Parent != null)
			{
				text = selectedNode.Parent.Text;
				text2 = selectedNode.Text;
			}
			else if (selectedNode.FirstNode != null)
			{
				text = selectedNode.Text;
				text2 = selectedNode.FirstNode.Text;
			}
			if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text2))
			{
				return;
			}
			this.dataGridView.Rows.Clear();
			foreach (BuildingPlaceData buildingPlaceData in this.GetBuildingPlaces(text, text2))
			{
				if ((buildingPlaceData.State & DataState.Deleted) == DataState.None)
				{
					this.AddNewRow(buildingPlaceData);
				}
			}
			if (this.dataGridView.Rows.Count > 0)
			{
				this.dataGridView.Rows[0].Selected = true;
			}
		}

		private DataGridViewRow AddNewRow(string industry, string buildingType)
		{
			BuildingPlaceData buildingPlaceData = new BuildingPlaceData();
			buildingPlaceData.State = DataState.New;
			buildingPlaceData.Industry = industry;
			buildingPlaceData.BuildingType = buildingType;
			DataGridViewRow dataGridViewRow = new DataGridViewRow();
			int index = this.dataGridView.Rows.Add(dataGridViewRow);
			dataGridViewRow = this.dataGridView.Rows[index];
			dataGridViewRow.Tag = buildingPlaceData;
			this.UpadteRowData(dataGridViewRow, false);
			return dataGridViewRow;
		}

		private DataGridViewRow AddNewRow(BuildingPlaceData data)
		{
			DataGridViewRow dataGridViewRow = new DataGridViewRow();
			int index = this.dataGridView.Rows.Add(dataGridViewRow);
			dataGridViewRow = this.dataGridView.Rows[index];
			dataGridViewRow.Tag = data;
			this.UpadteRowData(dataGridViewRow, false);
			return dataGridViewRow;
		}

		private void UpadteRowData(DataGridViewRow row, bool saveOrShow)
		{
			if (row == null)
			{
				return;
			}
			BuildingPlaceData buildingPlaceData = (BuildingPlaceData)row.Tag;
			if (buildingPlaceData == null)
			{
				return;
			}
			foreach (object obj in this.dataGridView.Columns)
			{
				DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj;
				if (saveOrShow)
				{
					buildingPlaceData[dataGridViewColumn.Name] = ((row.Cells[dataGridViewColumn.Name].Value != null) ? row.Cells[dataGridViewColumn.Name].Value : string.Empty);
				}
				else
				{
					row.Cells[dataGridViewColumn.Name].Value = buildingPlaceData[dataGridViewColumn.Name];
				}
			}
			if (saveOrShow)
			{
				buildingPlaceData.State |= DataState.Modified;
			}
		}

		private TreeNode GetTreeNodeByName(string industry, string buildingType)
		{
			if (string.IsNullOrWhiteSpace(industry) || string.IsNullOrWhiteSpace(buildingType))
			{
				return null;
			}
			foreach (object obj in this.treeView.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.GetNodeCount(false) > 0 && !(industry != treeNode.Text))
				{
					foreach (object obj2 in treeNode.Nodes)
					{
						TreeNode treeNode2 = (TreeNode)obj2;
						if (buildingType == treeNode2.Text)
						{
							return treeNode2;
						}
					}
				}
			}
			return null;
		}

		private void SetUserSelectedNode()
		{
			if (this.m_userId <= 0 && (string.IsNullOrWhiteSpace(this.m_userIndustry) || string.IsNullOrWhiteSpace(this.m_userBuildingType)))
			{
				return;
			}
			string text = string.Empty;
			string text2 = string.Empty;
			BuildingPlaceData buildingPlaceData = this.m_dataList.Find((BuildingPlaceData item) => item.Id == this.m_userId);
			if (buildingPlaceData != null)
			{
				text = buildingPlaceData.Industry;
				text2 = buildingPlaceData.BuildingType;
			}
			text = (string.IsNullOrWhiteSpace(text) ? this.m_userIndustry : text);
			text2 = (string.IsNullOrWhiteSpace(text2) ? this.m_userBuildingType : text2);
			if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(text2))
			{
				return;
			}
			TreeNode treeNodeByName = this.GetTreeNodeByName(text, text2);
			if (treeNodeByName == null)
			{
				return;
			}
			this.treeView.SelectedNode = treeNodeByName;
		}

		private void SetUserSelectedPlace()
		{
			if (this.m_userId <= 0 && string.IsNullOrWhiteSpace(this.m_userPlace))
			{
				return;
			}
			foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				BuildingPlaceData buildingPlaceData = (BuildingPlaceData)dataGridViewRow.Tag;
				if (buildingPlaceData != null && (this.m_userId == buildingPlaceData.Id || this.m_userPlace == buildingPlaceData.Place))
				{
					dataGridViewRow.Selected = true;
					break;
				}
			}
		}

		private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			e.Graphics.FillRectangle(Brushes.White, e.Node.Bounds);
			if (e.State == TreeNodeStates.Selected)
			{
				Brush brush = new SolidBrush(System.Drawing.Color.FromArgb(51, 153, 255));
				e.Graphics.FillRectangle(brush, new System.Drawing.Rectangle(e.Node.Bounds.Left, e.Node.Bounds.Top, e.Node.Bounds.Width, e.Node.Bounds.Height));
				System.Drawing.Rectangle r = new System.Drawing.Rectangle(e.Node.Bounds.Left, e.Node.Bounds.Top, e.Node.Bounds.Width + 10, e.Node.Bounds.Height);
				e.Graphics.DrawString(e.Node.Text, this.treeView.Font, Brushes.White, r);
				return;
			}
			e.DrawDefault = true;
		}

		private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.dataGridView.EndEdit();
			this.RefreshDataGridView();
		}

		private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
		{
			if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count || e.ColumnIndex < 0 || e.ColumnIndex >= this.dataGridView.ColumnCount)
			{
				e.Cancel = true;
				return;
			}
			object value = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
			this.m_oldCellText = ((value != null) ? value.ToString().Trim() : string.Empty);
		}

		private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView.Rows.Count || e.ColumnIndex < 0 || e.ColumnIndex >= this.dataGridView.ColumnCount)
			{
				return;
			}
			object value = this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
			string a = (value != null) ? value.ToString().Trim() : string.Empty;
			if (a == this.m_oldCellText)
			{
				return;
			}
			if (this.dataGridView.Columns[e.ColumnIndex].Name == "Place")
			{
				foreach (object obj in ((IEnumerable)this.dataGridView.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
					if (dataGridViewRow.Tag != null)
					{
						BuildingPlaceData buildingPlaceData = (BuildingPlaceData)dataGridViewRow.Tag;
						if (a == buildingPlaceData.Place)
						{
							MessageBox.Show("建筑照明标准值\"场所\"不能重复,请修改。", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.m_oldCellText;
							return;
						}
					}
				}
			}
			DataGridViewRow row = this.dataGridView.Rows[e.RowIndex];
			this.UpadteRowData(row, true);
			this.EnableButtonSave(true);
		}

		private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			string name = this.dataGridView.CurrentCell.OwningColumn.Name;
			if (name == "NowPD" || name == "TargetPD" || name == "IllEvenness")
			{
				e.Control.KeyPress -= this.DoubleTextBox_KeyPress;
				e.Control.KeyPress += this.DoubleTextBox_KeyPress;
				return;
			}
			if (name == "IllStandard" || name == "GlareValue" || name == "Ra")
			{
				e.Control.KeyPress -= this.IntTextBox_KeyPress;
				e.Control.KeyPress += this.IntTextBox_KeyPress;
				return;
			}
			e.Control.KeyPress -= this.DoubleTextBox_KeyPress;
			e.Control.KeyPress -= this.IntTextBox_KeyPress;
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			string text = string.Empty;
			string text2 = string.Empty;
			TreeNode selectedNode = this.treeView.SelectedNode;
			if (selectedNode == null)
			{
				return;
			}
			if (selectedNode.Parent != null)
			{
				text = selectedNode.Parent.Text;
				text2 = selectedNode.Text;
			}
			else if (selectedNode.FirstNode != null)
			{
				text = selectedNode.Text;
				text2 = selectedNode.FirstNode.Text;
			}
			if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text2))
			{
				return;
			}
			BuildingPlaceData item = (BuildingPlaceData)this.AddNewRow(text, text2).Tag;
			this.m_dataList.Add(item);
			this.EnableButtonSave(true);
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (this.dataGridView.SelectedRows.Count <= 0)
			{
				return;
			}
			if (this.dataGridView.Rows.Count == 1)
			{
				MessageBox.Show("建筑用途类型至少应该包含一种建筑场所照度标准值。", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			if (MessageBox.Show("确定要删除此建筑场所照度标准值？", "盈建科提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
			{
				return;
			}
			int index = this.dataGridView.SelectedRows[0].Index;
			DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
			if (dataGridViewRow.Tag == null)
			{
				this.dataGridView.Rows.RemoveAt(index);
			}
			else
			{
				((BuildingPlaceData)dataGridViewRow.Tag).State |= DataState.Deleted;
				this.dataGridView.Rows.RemoveAt(index);
			}
			this.dataGridView.ClearSelection();
			if (index >= this.dataGridView.Rows.Count)
			{
				this.dataGridView.Rows[0].Selected = true;
			}
			else
			{
				this.dataGridView.Rows[index].Selected = true;
			}
			this.EnableButtonSave(true);
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			BuildingPlaceDb.ThisDb.SaveDatas(this.m_dataList);
			this.ResetDataState();
			this.EnableButtonSave(false);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.dataGridView.EndEdit();
			BuildingPlaceDb.ThisDb.SaveDatas(this.m_dataList);
			if (this.dataGridView.SelectedRows.Count > 0)
			{
				DataGridViewRow dataGridViewRow = this.dataGridView.SelectedRows[0];
				this.m_dataPlace = ((dataGridViewRow.Tag != null) ? ((BuildingPlaceData)dataGridViewRow.Tag) : null);
			}
			base.DialogResult = DialogResult.OK;
		}

		private void IntTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r')
			{
				e.Handled = true;
			}
		}

		private void DoubleTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (sender is System.Windows.Forms.TextBox)
			{
				System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
				if (textBox == null)
				{
					return;
				}
				string text = textBox.Text;
				if (e.KeyChar == '.' && text.IndexOf(".") >= 0)
				{
					e.Handled = true;
				}
			}
		}

		private void ResetDataState()
		{
			List<BuildingPlaceData> list = new List<BuildingPlaceData>();
			foreach (BuildingPlaceData buildingPlaceData in this.m_dataList)
			{
				if ((buildingPlaceData.State & DataState.Deleted) == DataState.None)
				{
					buildingPlaceData.State = DataState.Ready;
					list.Add(buildingPlaceData);
				}
			}
			this.m_dataList = list;
		}

		private void EnableButtonSave(bool flag)
		{
			this.btnSave.Enabled = flag;
		}

		private List<BuildingPlaceData> m_dataList;

		private string m_userIndustry = string.Empty;

		private string m_userBuildingType = string.Empty;

		private string m_userPlace = string.Empty;

		private int m_userId = -1;

		private string m_oldCellText = string.Empty;

		private BuildingPlaceData m_dataPlace;
	}
}
